package com.windward.vii.dao.mapper;

import com.windward.vii.dao.entity.Organization;
import org.apache.ibatis.annotations.*;

import java.util.List;
import java.util.Map;

public interface OrganizationMapper {
    @Delete({
        "delete from organization",
        "where id = #{id,jdbcType=BIGINT}"
    })
    int deleteByPrimaryKey(Long id);

    @Insert({
        "insert into organization (id, name, ",
        "profit_rate, user_count, ",
        "income_amount, unbalanced_amount, ",
        "area, city, create_time, ",
        "update_time)",
        "values (#{id,jdbcType=BIGINT}, #{name,jdbcType=VARCHAR}, ",
        "#{profitRate,jdbcType=DECIMAL}, #{userCount,jdbcType=INTEGER}, ",
        "#{incomeAmount,jdbcType=BIGINT}, #{unbalancedAmount,jdbcType=BIGINT}, ",
        "#{area,jdbcType=VARCHAR}, #{city,jdbcType=VARCHAR}, #{createTime,jdbcType=TIMESTAMP}, ",
        "#{updateTime,jdbcType=TIMESTAMP})"
    })
    int insert(Organization record);

    int insertSelective(Organization record);

    @Select({
        "select",
        "id, name, profit_rate, user_count, income_amount, unbalanced_amount, area, city, ",
        "create_time, update_time",
        "from organization",
        "where id = #{id,jdbcType=BIGINT}"
    })
    @ResultMap("BaseResultMap")
    Organization selectByPrimaryKey(Long id);

    int updateByPrimaryKeySelective(Organization record);

    @Update({
        "update organization",
        "set name = #{name,jdbcType=VARCHAR},",
          "profit_rate = #{profitRate,jdbcType=DECIMAL},",
          "user_count = #{userCount,jdbcType=INTEGER},",
          "income_amount = #{incomeAmount,jdbcType=BIGINT},",
          "unbalanced_amount = #{unbalancedAmount,jdbcType=BIGINT},",
          "area = #{area,jdbcType=VARCHAR},",
          "city = #{city,jdbcType=VARCHAR},",
          "create_time = #{createTime,jdbcType=TIMESTAMP},",
          "update_time = #{updateTime,jdbcType=TIMESTAMP}",
        "where id = #{id,jdbcType=BIGINT}"
    })
    int updateByPrimaryKey(Organization record);

    /***********以下为自定义方法************************/

    List<Organization> getList(Map map);

    @Select({
            "select *",
            "from organization",
            "where name = #{name,jdbcType=VARCHAR}"
    })
    @ResultMap("BaseResultMap")
    Organization getByName(String name);

    // 主播聊天和收礼物，增加机构的收入
    @Update({
            "update organization",
            "set",
            "income_amount = income_amount + #{coin,jdbcType=BIGINT}",
            "where id = #{id,jdbcType=BIGINT}"
    })
    int income(@Param("id") Long id, @Param("coin") Integer coin);

    // 用户消费-聊天和收礼物，减少机构的收入
    @Update({
            "update organization",
            "set",
            "income_amount = income_amount - #{coin,jdbcType=BIGINT}",
            "where id = #{id,jdbcType=BIGINT}"
    })
    int consume(@Param("id") Long id, @Param("coin") Integer coin);
}